#!/usr/bin/make -f
# debian/rules for Free Pascal

# Documentation type to use pdf/html
DOCTYPE=pdf

# Detect name of new compiler, take care that debian uses amd64 instead of x86_64
CPU_SOURCE=$(subst amd64,x86_64,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
CPU_TARGET=$(subst amd64,x86_64,$(shell dpkg-architecture -qDEB_HOST_ARCH))

ifeq ($(CPU_TARGET),m68k)
PPSUF=68k
endif
ifeq ($(CPU_TARGET),i386)
PPSUF=386
endif
ifeq ($(CPU_TARGET),x86_64)
PPSUF=x64
endif
ifeq ($(CPU_TARGET),powerpc)
PPSUF=ppc
endif
ifeq ($(CPU_TARGET),alpha)
PPSUF=axp
endif
ifeq ($(CPU_TARGET),arm)
PPSUF=arm
endif
ifeq ($(CPU_TARGET),sparc)
PPSUF=sparc
endif

ifneq ($(CPU_SOURCE),$(CPU_TARGET))
PPPRE=ppcross
else
PPPRE=ppc
endif

PPNEW=$(PPPRE)$(PPSUF)

# Reset FPC and FPCDIR if it was set
FPC=
FPCDIR=
# Get Package version and FPC version out of changelog file
PACKAGEVERSION:=$(shell head -n 1 debian/changelog | awk '{ print $$2 }' | tr -d '[()]')
FPCVERSION:=$(shell echo $(PACKAGEVERSION) | awk -F '-' '{ print $$1 }')
FPCTARGET=$(CPU_TARGET)-linux
# Get directories
PWD:=$(shell pwd)
BUILD_DIR=$(PWD)/debian/build
INSTALL_DIR=$(PWD)/debian/tmp
DOC_DIR=$(INSTALL_DIR)/usr/share/doc
EXAMPLE_TEMP=$(DOC_DIR)/fpc-$(FPCVERSION)
# Get utils
NEWPP=$(PWD)/compiler/$(PPNEW)
NEWFPDOC=$(PWD)/utils/fpdoc/fpdoc
NEWFPCMAKE=$(PWD)/utils/fpcm/fpcmake
# Create default options
BUILDOPTS=PP=$(NEWPP)
INSTALLOPTS=PP=$(NEWPP) FPCMAKE=$(NEWFPCMAKE) INSTALL_PREFIX=$(INSTALL_DIR)/usr

# export DH_VERBOSE=1

###################
# Clean
#

clean:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp install-arch-stamp arrange-arch-stamp
	rm -f build-indep-stamp install-indep-stamp

	rm -f debian-files-stamp
	rm -f debian/*.files debian/*.docs debian/*.examples debian/*.postinst debian/*.postrm

	$(MAKE) compiler_distclean
	$(MAKE) rtl_distclean
	$(MAKE) packages_distclean
	$(MAKE) fcl_distclean
	$(MAKE) fv_distclean
	$(MAKE) ide_distclean
	$(MAKE) utils_distclean

	$(MAKE) -C fpcdocs clean

	-(find . -name \*.a | xargs rm)

	dh_clean

# Cannot do that because the control-file is often needed after a cleanup
#	rm -f debian/control


###################
# Debian files
#

debian-files: debian-files-stamp
debian-files-stamp:
	$(RM) debian/control.in debian/fp-compiler.conffiles
	@echo "--- Creating debian files"
	bash debian/fixdeb debian $(FPCTARGET) $(PPNEW)

	touch debian-files-stamp


###################
# Arch packages
#

build-arch: build-arch-stamp
build-arch-stamp: debian-files-stamp
	@echo "--- Building"
	dh_testdir

# First make a new Compiler and RTL using a make cycle
	$(MAKE) compiler_cycle
	$(MAKE) rtl_clean rtl_smart $(BUILDOPTS)
	$(MAKE) packages_base_smart $(BUILDOPTS)
	$(MAKE) fcl_smart $(BUILDOPTS)
	$(MAKE) fv_smart $(BUILDOPTS)
	$(MAKE) packages_extra_smart $(BUILDOPTS)
	$(MAKE) ide_all $(BUILDOPTS)
	$(MAKE) utils_all $(BUILDOPTS)

	touch build-arch-stamp


install-arch: install-arch-stamp
install-arch-stamp: build-arch-stamp debian-files-stamp
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_clean -k

# Specify the compiler to use so installing will use the correct versioned dir
	$(MAKE) compiler_distinstall $(INSTALLOPTS)
	$(MAKE) rtl_distinstall $(INSTALLOPTS)
	$(MAKE) fcl_distinstall $(INSTALLOPTS)
	$(MAKE) fv_distinstall $(INSTALLOPTS)
	$(MAKE) packages_distinstall $(INSTALLOPTS)
	$(MAKE) ide_distinstall $(INSTALLOPTS) INSTALL_DOCDIR=$(DOC_DIR)/fp-ide
	$(MAKE) utils_distinstall $(INSTALLOPTS)

# Copy examples to the correct doc dir
	bash debian/moveexamples $(EXAMPLE_TEMP) $(DOC_DIR)

# Install man pages and RTL demos and whatsnew and readme
	$(MAKE) -C man installman $(INSTALLOPTS) INSTALL_PREFIX=$(INSTALL_DIR)/usr/share
	$(MAKE) -C demo sourceinstall $(INSTALLOPTS) INSTALL_SOURCEDIR=$(DOC_DIR)/fp-compiler
	$(MAKE) -C doc installdoc $(INSTALLOPTS) INSTALL_DOCDIR=$(DOC_DIR)/fp-compiler

# Create fpc.cfg which is included as conffile
	chmod 755 compiler/utils/samplecfg
	compiler/utils/samplecfg "/usr/lib/fpc/\$$fpcversion" $(INSTALL_DIR)/etc
	touch install-arch-stamp


arrange-arch: arrange-arch-stamp
arrange-arch-stamp: install-arch-stamp debian-files-stamp
	dh_testdir
	dh_testroot
	dh_install -a --sourcedir=debian/tmp --list-missing

	touch arrange-arch-stamp


###################
# Documentation
#

build-indep: build-indep-stamp
# We depend on the new fpdoc
build-indep-stamp: debian-files-stamp build-arch-stamp
	@echo "--- Building Documentation"
	dh_testdir

	$(MAKE) -C fpcdocs $(DOCTYPE) $(BUILDOPTS) FPDOC=$(NEWFPDOC)

	touch build-indep-stamp


install-indep: install-indep-stamp
install-indep-stamp: debian-files build-indep
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	dh_clean -k

	$(MAKE) -C fpcdocs $(DOCTYPE)install $(INSTALLOPTS) INSTALL_DOCDIR=$(INSTALL_DIR)/usr/share/doc/fp-docs

	touch install-indep-stamp


###################
# Generic
#

build: build-arch build-indep

install: install-arch install-indep

binary: binary-arch binary-indep


###################
# Deb building
#

binary-indep: build-indep install-indep debian-files
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
#	dh_installchangelogs -pfp-docs logs/Changes.docs
	dh_install -i --sourcedir=debian/tmp --list-missing
	dh_compress -i -X.pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch arrange-arch debian-files
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_installdocs -a
	dh_installchangelogs -a

#	dh_installchangelogs -pfp-compiler logs/Changes.compiler
#	dh_installchangelogs -pfp-units-rtl logs/Changes.rtl
#	dh_installchangelogs -pfp-units-fcl logs/Changes.fcl
#	dh_installchangelogs -pfp-utils logs/Changes.utils

	dh_installexamples -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


.PHONY: build clean binary debian-files \
        build-arch install-arch binary-arch \
        binary-indep
